//
//  ErrorView.m
//  LoadImageAnimation
//
//  Created by Burn on 2019/3/20.
//  Copyright © 2019 menksoft. All rights reserved.
//

#import "ErrorView.h"

@interface ErrorView()


@property(strong,nonatomic) UILabel *msg;
@property(assign,nonatomic) NSTimeInterval after;
@property(copy,nonatomic) NSString *text;

@end


@implementation ErrorView

- (void)showInView:(UIView *)view msg:(NSString *)msg after:(NSTimeInterval)after{
    self.text = msg;
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    
//    UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//    UINavigationController *nav = [tabbarController.childViewControllers firstObject];
//    CGRect statuF = [UIApplication sharedApplication].statusBarFrame;
//    CGRect navF = nav.navigationBar.frame;
    
    CGSize maximumLableSize = CGSizeMake(MAXFLOAT, 30);
    CGSize expectSize = [self.msg sizeThatFits:maximumLableSize];
    
    self.after = after;
    //    self.frame = CGRectMake(0, 0, expectSize.height + 10, 100);
    self.frame = CGRectMake(0, 0, 30, expectSize.width + 10);
    self.center = view.center;
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.7];
    self.layer.cornerRadius = 3;
    self.layer.masksToBounds = YES;
    [view addSubview:self];
    
    
    self.msg.frame = CGRectMake(0, 5,30,expectSize.width + 10);
    [self addSubview:self.msg];
    
    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(after * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.5 animations:^{
                self.alpha = 0;
            } completion:^(BOOL finished) {
                [self removeFromSuperview];
                self.msg = nil;
            }];
        });
    });
}

- (UILabel *)msg{
    if (!_msg) {
        _msg = [[UILabel alloc]init];
        _msg.transform = CGAffineTransformScale(_msg.transform, -1, 1);
        _msg.transform =  CGAffineTransformRotate(_msg.transform, M_PI / 2);
        _msg.font = [UIFont fontWithName:@"HvrStudioChaganTig-M" size:24];
        _msg.textColor = [UIColor whiteColor];
        _msg.numberOfLines = 0;
        _msg.text = self.text.length == 0 ? @" " : self.text;
        //        _msg.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.7];
        _msg.backgroundColor = [UIColor clearColor];
        //        _msg.textAlignment = NSTextAlignmentCenter;
        //        _msg.layer.cornerRadius = 3;
        //        _msg.layer.masksToBounds = YES;
    }
    return _msg;
}
@end


